package com.shanling.shanlingcontroller.ui.fragment;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.text.TextUtils;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.shanling.shanlingcontroller.R;
import com.shanling.shanlingcontroller.adapter.CommonAdapter;
import com.shanling.shanlingcontroller.adapter.ViewHolder;
import com.shanling.shanlingcontroller.base.BaseMVPFragment;
import com.shanling.shanlingcontroller.bean.Album;
import com.shanling.shanlingcontroller.bean.Artist;
import com.shanling.shanlingcontroller.bean.Constants;
import com.shanling.shanlingcontroller.bean.EventCenter;
import com.shanling.shanlingcontroller.persenter.AlbumFragmentPersenter;
import com.shanling.shanlingcontroller.persenter.contract.AlbumFragmentConttact;
import com.shanling.shanlingcontroller.ui.activity.PlaylistDetailActivity;
import com.shanling.shanlingcontroller.utils.ToastUtils;
import com.shanling.shanlingcontroller.view.ClearableEditText;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Created by ch1 on 2019/11/13.
 */

public class AlbumFragment extends BaseMVPFragment<AlbumFragmentConttact.Presenter> implements AlbumFragmentConttact.View, SwipeRefreshLayout.OnRefreshListener, AdapterView.OnItemClickListener, TextView.OnEditorActionListener {
    @BindView(R.id.et_song)
    ClearableEditText etSong;
    @BindView(R.id.rl_song)
    RelativeLayout rlSong;
    @BindView(R.id.lv_albums)
    ListView lvAlbums;
    @BindView(R.id.sw)
    SwipeRefreshLayout sw;
    @BindView(R.id.bt_scan)
    Button btScan;
    @BindView(R.id.ll_nomusic)
    LinearLayout llNomusic;
    @BindView(R.id.pb)
    ProgressBar pb;
    private CommonAdapter<Album> myAdapter;
    private List<Album> list = new ArrayList<>();
    private boolean isRefresh = false;

    @Override
    protected void onLazyLoad() {
        isRefresh = false;
        mPresenter.loadAlbums("all");
    }
    @Override
    protected int getContentViewLayoutID() {
        return R.layout.fragment_album;
    }

    @Override
    protected void initViewsAndEvents() {
        sw.setColorSchemeColors(0xffC8B492, 0xFF444444);
        sw.setOnRefreshListener(this);
        myAdapter = new CommonAdapter<Album>(getActivity(), list, R.layout.item_singer) {
            @Override
            public void convert(ViewHolder helper, int position, Album item) {
                ImageView iv_singer=helper.getView(R.id.iv_singer);
                TextView tv_singer=helper.getView(R.id.tv_singer);
                TextView tv_count=helper.getView(R.id.tv_count);
                if(item.getCover()==null){
                    Glide.with(getActivity()).load(R.drawable.album_img_unknown).into(iv_singer);
                }else {
                    Glide.with(getActivity()).load(item.getCover()).error(R.drawable.album_img_unknown).into(iv_singer);
                }
                if(TextUtils.isEmpty(item.getName())||item.getName().equals("<unknown>")){
                    tv_singer.setText(R.string.unknown);
                }else {
                    tv_singer.setText(item.getName());
                }
                tv_count.setText(item.getCount()+getString(R.string.songs));
            }
        };
        lvAlbums.setAdapter(myAdapter);
        lvAlbums.setOnItemClickListener(this);
        etSong.setOnEditorActionListener(this);
    }
    @Override
    protected boolean isBindEventBusHere() {
        return false;
    }

    @Override
    protected void onEventComing(EventCenter eventCenter) {
//        switch (eventCenter.getEventCode()) {
//            case Constants.PLAYLISTEVENT:
//                isRefresh = true;
//                mPresenter.loadAlbums("all");;
//                break;
//        }

    }

    @Override
    public void setEmptyView() {
        llNomusic.setVisibility(View.VISIBLE);
        pb.setVisibility(View.GONE);
        sw.setVisibility(View.INVISIBLE);
        sw.setRefreshing(false);

    }

    @Override
    public void showLoading() {
        if (!isRefresh){
            pb.setVisibility(View.VISIBLE);
        }
        llNomusic.setVisibility(View.INVISIBLE);

    }

    @Override
    public void hideLoading() {
        pb.setVisibility(View.GONE);
        sw.setRefreshing(false);

    }
    @Override
    public void showAlbums(List<Album> albumList) {
        llNomusic.setVisibility(View.INVISIBLE);
        sw.setRefreshing(false);
        pb.setVisibility(View.GONE);
       list.clear();
        list.addAll(albumList);
        myAdapter.refresh(list);
    }

    @Override
    protected AlbumFragmentConttact.Presenter createPresenter() {
        return new AlbumFragmentPersenter();
    }


    @OnClick(R.id.bt_scan)
    public void onViewClicked() {
        isRefresh =false;
        mPresenter.loadAlbums("all");
    }
    @Override
    public void onRefresh() {
        isRefresh =true;
        mPresenter.loadAlbums("all");
    }
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Bundle bundle=new Bundle();
        bundle.putSerializable("Album",list.get(position));
        bundle.putInt("type",2);
        readyGo(PlaylistDetailActivity.class,bundle);
    }
    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        if(actionId== EditorInfo.IME_ACTION_SEARCH||(event!=null&&event.getKeyCode()== KeyEvent.KEYCODE_ENTER)){
            String trim = etSong.getText().toString().trim();
            if(TextUtils.isEmpty(trim)){
                ToastUtils.showToast(getActivity(),R.string.Search_localsongs);
                return true;
            }
            List<Album> datas = new ArrayList<>();
            for (int i = 0; i <list.size() ; i++) {
                if(list.get(i).getName().contains(trim)){
                    datas.add(list.get(i));
                }
            }
            list.clear();
            list.addAll(datas);
            myAdapter.refresh(list);
            return true;
        }
        return false;
    }
}
